const { mysql: config } = require('../config')
const { mysql } = require('../qcloud')

module.exports = async (ctx) => {
  const { openId, bookIsbn, comment, location, phone} = ctx.request.body
  const DB = mysql
  // const DB = require('knex')({
  //   client: 'mysql',
  //   connection: {
  //     host: config.host,
  //     port: config.port,
  //     user: config.user,
  //     password: config.pass,
  //     database: config.db,
  //     charset: config.char,
  //     multipleStatements: true
  //   }
  // })
  // insert into comments(id, open_id, book_isbn, comment, phone, location) value(1, 1, 'test2', 'test3', 'test4', 'test5')
  try {
    await DB('comments').insert({open_id: openId, book_isbn: bookIsbn, comment, location, phone})
    ctx.state.data = {
      msg: '评论成功'
    }
  } catch (e) {
    ctx.state = {
      code: -1,
      data: {
        msg: '评论失败:' + e.sqlMessage
      }
    }
  }

}
